﻿using Eps.Ai.Common;
using InfonavaEye.components;
using InfonavaEye.Forms;
using InfonavaEye.handler;
using InfonavaEye.Handler;
using InfonavaEye.model;
using InfonavaEye.Service;
using InfonavaEye.model.wjapi;
using InfonavaEye.util;
using Sylan.Common.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace InfonavaEye
{
    public partial class IndexForm : Form
    {
       
        public IndexForm()
        {
            InitializeComponent();
            //this.checkMac();
        }

        /// <summary>
        /// 检查本机MAC地址是否正确
        /// </summary>
        private void checkMac()
        {
            var mac = SystemInfoHelper.getMacAddr_Local();
            Console.WriteLine("Config.DEVICE_SN", Config.DEVICE_SN);
            Console.WriteLine("Config.DEVICE_MODEL", Config.DEVICE_MODEL);
            Console.WriteLine("Config.DEVICE_MAC", Config.DEVICE_MAC);
            if (!mac.Equals(Config.DEVICE_MAC))
            {
                MessageBox.Show("检测到非法使用本程序，请联系程序的所有者");
                System.Environment.Exit(0);
            }
        }

        private void pictureBox2_Paint(object sender, PaintEventArgs e)
        {

        }

        private void IndexForm_Load(object sender, EventArgs e)
        {
            
            //this.WindowState = FormWindowState.Maximized;


            /*ThreadHelper.ExcuteThreadTask(() =>
            {
                startHttpsBoltStraight();
            }, 250);*/

            //磁盘检测
            Task.Factory.StartNew(() =>
            {
                this.checkDiskSize();

                if (Config.OCR_MODEL == (int)OCRModelEnum.SERVER)
                {
                    //AliyunOSSSrvice.Instance.initAliyunOssClient();
                   // WujingConfigService.getStorageConfig();
                }
            });
            //升级检查
            this.checkUpgrade();
           
        }


        private void IndexForm_FormClosing(object sender, FormClosingEventArgs e)
        {
           
        }

        private string exePath = null;
        private void startHttpsBoltStraight()
        {
            exePath = util.FileHelper.getBaseDir() + "\\socket_multi_process_server\\socket_multi_process_server.exe";
            ExeUtil.openExe(exePath, "圆型图片拉直");
        }

        private void checkDiskSize()
        {
            long freeSpace = GetHardDiskFreeSpace("D");
            string baseDir = util.FileHelper.getBaseDir() + "\\Image";
            if (freeSpace < Config.MAX_HARD_DISK_FREE_SPACE)
            {
                MessageBox.Show(String.Format("检测到电脑的D盘控件只剩下{0}G了，请清理不再需要的文件,位置在：{1}。", freeSpace, baseDir));
            }
        }

        /// <summary>
        /// 获取指定驱动器的剩余空间总大小(单位为GB)
        /// </summary>
        /// <param name="str_HardDiskName"></param>
        /// <returns>GB</returns>        
        private long GetHardDiskFreeSpace(string str_HardDiskName)
        {
            long freeSpace = new long();
            str_HardDiskName = str_HardDiskName + ":\\";
            System.IO.DriveInfo[] drives = System.IO.DriveInfo.GetDrives();
            foreach (System.IO.DriveInfo drive in drives)
            {
                if (drive.Name == str_HardDiskName)
                {
                    freeSpace = drive.TotalFreeSpace / (1024 * 1024 * 1024);
                }
            }
            return freeSpace;
        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {
            ManualCaptureForm mainForm = new ManualCaptureForm();
            mainForm.Show();
        }

        private void pictureBox3_Click(object sender, EventArgs e)
        {
            AutoCaptureForm mainForm = new AutoCaptureForm();
            mainForm.Show();
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            TemplateListForm form = new TemplateListForm();
            form.ShowDialog();
        }

        private void deviceStatusBox_Click(object sender, EventArgs e)
        {

        }

        private void IndexForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            System.Environment.Exit(0);
        }

        /// <summary>
        /// 检测是否需要升级
        /// </summary>
        public void checkUpgrade() {

            /*   UpgradeForm uf = new UpgradeForm();
               uf.ShowDialog();*/
            SaasApiCommonResp<Upgrade> resp = HttpTools.getInstance().request<SaasApiCommonResp<Upgrade>>("", Config.SERIVE_SAASV2_DOMAIN + "/ucpm/app/upgrade?appId=IE20230504&clientType=ie&version=1.2", "GET");
            if (resp == null)
            {
                return;
            }
            if (resp.data != null && resp.data.update&&
                !String.IsNullOrEmpty(resp.data.wgtUrl) 
                && compareVersion(Config.APP_VERSION, resp.data.version))
            {
                //有更新 ，弹出更新窗体（暂时先实现强制升级，后续再优化成可选升级）
                UpgradeForm uf = new UpgradeForm();
                uf.upgradeData(resp.data);
                uf.Show();
            }
        }

        public bool compareVersion(string oldVersion, string newVersion)
        {
            if (string.IsNullOrEmpty(oldVersion) || string.IsNullOrEmpty(newVersion))
            {
                return false;
            }
            oldVersion = oldVersion.Replace("V","");
            oldVersion = oldVersion.Replace("v", "");
            Version v = new Version(oldVersion);
            Version nv = new Version(newVersion);
            if (nv > v)
            {
                return true;
            }
            return false;
        }
    }
}
